import fnmatch
import hashlib
import os

# TODO: Cache this response.

def processFiles(dir, pattern, arr, hash):
  for root, dirnames, filenames in os.walk(dir):
    for filename in fnmatch.filter(filenames, pattern):
      absoluteFilename = os.path.join(root, filename)
      arr.append(absoluteFilename.replace('applicationfiles/', ''))
      addFileToHash(absoluteFilename, hash)

def addFileToHash(filename, hash):
  file = open(filename)
  hash.update(file.read())
  file.close()

filenames = []
hash = hashlib.md5()

addFileToHash('index.html', hash)
addFileToHash('helloworld.py', hash)

processFiles('applicationfiles/css', '*.css', filenames, hash)
processFiles('applicationfiles/js', '*.js', filenames, hash)
processFiles('applicationfiles/soy', '*.js', filenames, hash)
processFiles('applicationfiles/images', '*', filenames, hash)

print 'Content-Type: text/cache-manifest'
print ''
print 'CACHE MANIFEST'
print '# Version: ' + hash.hexdigest()
print ''
print 'CACHE:'
for file in filenames:
  print file
print ''
print 'NETWORK:'
print '*'
print ''
print 'FALLBACK:'
